﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace FacePassword
{
    /// <summary>
    /// TODO : mainpulate anything about User table.
    /// </summary>
    class UserManager
    {
       
        public bool addUser(string Fname, string Lname, string email, string Birthdate,string Gender,  string mobile, string password)
         {
            SqlConnection con = new SqlConnection("Data Source=ReemMandour;Initial Catalog= Face Password;Integrated Security=SSPI");
            con.Open();
                              
            string insertUser = @"insert into  User_info (FName,LName , Gender, E_mail , BirthDate, Mobile, UPassword)
                                  values(@Fname, @Lname, @Gender, @email, @Birthdate, @mobile,  @password)";                                  
            SqlCommand insertcommand = new SqlCommand(insertUser, con);
            SqlParameter FName = new SqlParameter("@Fname", Fname );
            insertcommand.Parameters.Add(FName);
            SqlParameter LName = new SqlParameter("@Lname", Lname);
            insertcommand.Parameters.Add(LName);
            SqlParameter Email = new SqlParameter("@email", email);
            insertcommand.Parameters.Add(Email);
            SqlParameter birthDate = new SqlParameter("@Birthdate", Birthdate);
            insertcommand.Parameters.Add(birthDate);
            SqlParameter gender = new SqlParameter("@Gender", Gender);
            insertcommand.Parameters.Add(gender);
            SqlParameter Mobile = new SqlParameter("@mobile", mobile);
            insertcommand.Parameters.Add(Mobile);
            SqlParameter UPassword = new SqlParameter("@password", password);
            insertcommand.Parameters.Add(UPassword);

            insertcommand.ExecuteNonQuery();
            
            con.Close();
            return true;
           
        }

        /// <summary>
        /// this function retrieve user by email
        /// params: email
        /// return : datareader with all data of this user whose e_mail he has
        /// if exist return record 
        /// else return null
        /// </summary>
        public SqlDataReader RetrieveUserByE_mail(string email)
        {
            SqlConnection con = new SqlConnection("Data Source=AMIRAASAYED-PC\\SQLEXPRESS;Initial Catalog='Face Password';Integrated Security=True");
            con.Open();


            SqlCommand cmd = new SqlCommand("Select * From User_info where E_mail = @email", con);
            cmd.CommandType = CommandType.Text;
            SqlParameter email_par = new SqlParameter("@email", email);
            cmd.Parameters.Add(email_par);

            SqlDataReader reader = cmd.ExecuteReader();

            reader.Close();
            con.Close();
            if (reader == null)
                return null;
            else
                return reader;

        }
    }
}
